package com.lagou.rpc.consumer.task;

import com.lagou.rpc.util.ZkUtil;

import java.util.Map;
import java.util.Set;

/**
 * @Author : liuchangling
 * @Descrition :
 * @Date： Created in 3:05 下午 2021/12/10
 */
public class Task implements Runnable {
    @Override
    public void run() {
        ZkUtil zkUtil = new ZkUtil();
        //此时key为节点路径,value为节点的具体存储信息
        Map<String, String> addressList = zkUtil.getAddressList();
        Set<Map.Entry<String, String>> entrySet = addressList.entrySet();
        for (Map.Entry<String, String> entry : entrySet) {
            String nodeAddress = entry.getKey();
            String nodeValue = entry.getValue();
            System.out.println("节点"+nodeAddress+"的内容为"+nodeValue);

            //取出上次请求的时间
            Long lastTime = Long.valueOf(nodeValue);
            long currentTime = System.currentTimeMillis();
            if(currentTime-lastTime>5000){
                long timeMillis = System.currentTimeMillis();
                System.out.println("节点"+nodeAddress+"上一次请求时间超过五秒，内容将被重置，新节点内容为"+timeMillis+"-0");
                zkUtil.updateNodeData(nodeAddress,(timeMillis+"-0"));
            }
        }


    }
}
